package com.yiboshi.rehrs.helper.core;

import com.alibaba.fastjson.JSONObject;
import com.yiboshi.rehrs.common.ZhongConfig;
import com.yiboshi.rehrs.exception.ZyantExceptionUtils;
import com.yiboshi.rehrs.helper.ProxyHelper;
import com.yiboshi.rehrs.helper.ZhongConfigHelper;
import com.yiboshi.rehrs.util.CacheUtils;
import com.yiboshi.rehrs.util.JinsheUtil;
import com.yiboshi.rehrs.util.OkHttpUtil;
import com.yiboshi.rehrs.util.StrUtils;
import com.yiboshi.rehrs.util.js.AesUtils;
import com.yiboshi.rehrs.util.js.Qi5uxeel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class HeaderHelperJinshe implements HeaderHelper {

    @Autowired
    ZhongConfigHelper configHelper;

    @Autowired
    ProxyHelper proxyHelper;

    @Override
    public Map<String, String> incept(String type,int count) throws Exception {
        //获取n1
        String n1Key = "jsqi5:n1";
        String n1 = CacheUtils.getByCache10Min(n1Key, String.class);
        if(StrUtils.isBlank(n1)){
            n1 = Qi5uxeel.buildN1();
            CacheUtils.putByCache10Min(n1Key, n1);
        }
        ZhongConfig zhongConfig = getZhongConfig();
        String timeAes = zhongConfig.getUriMap().get("timeAes");
        String apiPhp =zhongConfig.getUriMap().get("apiPhp");
        String path = zhongConfig.getOtherMap().get(type);
        String encrypt = AesUtils.encrypt(path,timeAes).replaceAll("\n","");
        encrypt = URLEncoder.encode(encrypt,"utf-8");
        String data = encrypt;
        String encrypt2 = AesUtils.encrypt(n1, timeAes).replaceAll("\n","");
        String sign = URLEncoder.encode(encrypt2,"utf-8");;
        long currentTimeMillis = (System.currentTimeMillis()+500) / 1000;
        String ua = JinsheUtil.buildUA(currentTimeMillis);
        String requestStr = String.format("data=%s&sign=%s", data, sign);
        //requestStr = "data=j65E%2B6i%2BwAdkHLvxnVbDAU5b2KIoITtCaEiGdxVJf1vFildonhs79dcveMFtDbTr&sign=kjrOex4Kl%2BXE01Nh3qifwDFWhtqsrcewGOsAG9gSaxk%3D";
        OkHttpUtil post = new OkHttpUtil().url(apiPhp)
                .timeoutMillis(500)
                .retryCount(count)
                .addHeader("User-Agent", ua)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .proxySupplier(() -> proxyHelper.one())
                .post(requestStr.getBytes());
        String postPiliangRes = post.execute();
        if(StrUtils.isNotBlank(postPiliangRes)&&postPiliangRes.contains("<html>")){
            return null;
        }
        String postPiliang = AesUtils.decrypt(postPiliangRes,timeAes);
        JSONObject jsonObject = JSONObject.parseObject(postPiliang);
        Map<String, String> stringStringMap = new HashMap<>();
        String data1 = jsonObject.getString("data");
        ZyantExceptionUtils.x(StrUtils.isBlank(data1),"授权异常");
        stringStringMap.put("qi5uxeel",data1 );
        stringStringMap.put("cps-magic", jsonObject.getString("cps"));
        return stringStringMap;
    }



    public void commonMap(Map<String,String> stringMap,String type){
        stringMap.put("pda_version","248");
        stringMap.put("pda_network","3");
        stringMap.put("version_name","1.4.9");
    }



    private ZhongConfig getZhongConfig() {
        ZhongConfig config = configHelper.getConfig(3);
        return config;
    }
}
